JS 依照元素出現數量排序
同樣是最近遇到的狀況,需要利用陣列內元素出現的數量進行排序,以下為範例程式:
var array = ["1", "2", "4", "5", "2", "4", "2", "3"]; var str = array.toString(); var filter = []; var result = []; document.write("原始陣列:" + array + "<br>"); var get = function (str, tar, arr, tmp) { if (str.indexOf(tar) >= 0) { tmp = str.slice(str.indexOf(tar) + 1); arr.push(tar); get(tmp, tar, arr, tmp); } return arr; } for (i in array) { var elm = []; var tmp; var fstr = filter.join(); if (fstr.indexOf(array[i]) >= 0) { continue; } else { var tmp_arr = get(str, array[i], elm, tmp); result.push(tmp_arr.length + ':' + tmp_arr[0]); filter.push(array[i]); } } result.sort(); result.reverse(); //按照出現次數排序,不刪除重複元素 var finishArray = []; for (index in result) { var show = result[index].split(':'); for (i = 0; i < show[0]; i++) { finishArray.push(show[1]); } } document.write("按出現次數排序,不刪除重複元素:" + finishArray + "<br>"); //按照出現次數排序,刪除重複元素 var finishArray = []; for (index in result) { var show = result[index].split(':'); finishArray.push(show[1]); } document.write("按出現次數排序,刪除重複元素:" + finishArray + "<br>");
輸出結果:
原始陣列:1,2,4,5,2,4,2,3 按出現次數排序,不刪除重複元素:2,2,2,4,4,5,3,1 按出現次數排序,刪除重複元素:2,4,5,3,1
The mind brain injury law vancouver legal representatives at DE CARO & KAPLEN, LLP.
Guide To Online Shopping Sites In United Kingdom: The Intermediate Guide
Towards Online Shopping Sites In United Kingdom online shopping sites in united kingdom
where can you buy prednisone: prednisone buy canada – where can i get prednisone over the counter
The Comprehensive Guide To Erb’s Palsy Claim galveston erb’s palsy Law firm
The Secret Secrets Of Untreated Adhd In Adults Treat Adult Adhd
I am genuinely grateful to the owner of this web page who has shared this wonderful post
at here.
You’ll Never Be Able To Figure Out This 18 Wheeler Accident
Law Firm’s Benefits 18 wheeler accident law firm (Tania)
20 Questions You Should Have To Ask About Coffee Machines With Beans Before You Decide To Purchase It Coffee machine beans to Cup
Situs Sabung Ayam Digmaan
how much is generic neurontin: neurontin price uk – neurontin 30 mg
Why You Should Not Think About Making Improvements To Your Double Glazed Units Manufacturers Near Me replace double glazed units
amoxicillin pills 500 mg: amoxicillin 500mg over the counter – buy amoxicillin 250mg
Ask Me Anything: 10 Responses To Your Questions About Upvc Windows Repair repair
upvc window (articlement.Com)
how can i order prednisone: buy prednisone from canada – prednisone 20mg price
Generally I do not read post on blogs, but I
would like to say that this write-up very compelled me to check out
and do it! Your writing style has been surprised me. Thank you, quite
nice article.
How To Make A Profitable Window Repair Near Even If You’re
Not Business-Savvy window repairs
5 Machines Coffee-Related Lessons From The Pros smart Home coffee Machines
Are You Responsible For An Birth Defect Lawsuit Budget?
10 Incredible Ways To Spend Your Money Birth Defect Lawsuits
The Most Underrated Companies To Monitor In The Online Shopping Sites For
Clothes Industry Vimeo.Com
Fantastic goods from you, man. I have have in mind your
stuff prior to and you are just extremely magnificent.
I really like what you’ve acquired right here, really like what you are stating and the way in which during which
you say it. You are making it entertaining and you continue to take care of to stay it smart.
I cant wait to learn far more from you. That is really a
great website.